package com.zzq.my.jetpacks.net.base


import com.ihsanbal.logging.Level
import com.ihsanbal.logging.LoggingInterceptor
import com.localebro.okhttpprofiler.OkHttpProfilerInterceptor
import com.safframework.http.interceptor.AndroidLoggingInterceptor
import com.zzq.my.jetpacks.net.Util.UrlConfig
import okhttp3.ConnectionPool
import okhttp3.OkHttpClient
import okhttp3.internal.platform.Platform
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit





/**
 * 描述：RetrofitClient 配置信息类
 * 作者：zhuangzeqin
 * 时间: 2021/5/21-9:23
 * 邮箱：zzq@eeepay.cn
 * 备注: 采用的是kt 单例模式
 */
private const val TAG = "RetrofitClient"
class RetrofitClient {
    companion object {
        const val TIMEOUT = 10L
        private lateinit var retrofit: Retrofit
        fun getInstance() = SingletonHolder.INSTANCE
    }

    private object SingletonHolder {
        val INSTANCE = RetrofitClient()
    }

    init {
        //初始化配置
//        initConfig()
    }

    /**
     * 初始化配置
     * [baseUrl]项目接口的基类url，以/结尾
     */
    fun initConfig(
        baseUrl: String = UrlConfig.OFFICIAL_BASEURL,
        okClient: OkHttpClient = getOkHttpClient()
    ): RetrofitClient {
        // 构建 OkHttpClient 时,将 OkHttpClient.Builder() 传入 with() 方法,进行初始化配置
//        val OkHttpClientInfo = ProgressManager.getInstance().with(okClient.newBuilder()).build()
        //debug 模式下添加拦截器
//        if (BuildConfig.DEBUG) {
            //okClient.newBuilder().addInterceptor(OkHttpProfilerInterceptor())
//        }
        retrofit = Retrofit.Builder()
            .client(okClient)
            .addConverterFactory(GsonConverterFactory.create())
            .baseUrl(baseUrl)
            .build()
        return this
    }

    /**
     * 创建okhttpClient 对象
     */
     fun getOkHttpClient(): OkHttpClient {
        return OkHttpClient.Builder()
            .callTimeout(TIMEOUT, TimeUnit.SECONDS) //完整的请求超时时长，从发起请求到接收返回数据
            .connectTimeout(TIMEOUT, TimeUnit.SECONDS) //与服务器建立连接时长
            .readTimeout(TIMEOUT, TimeUnit.SECONDS) //读取服务器返回数据的时长
            .writeTimeout(TIMEOUT, TimeUnit.SECONDS) //向服务器写入数据的时长
            .retryOnConnectionFailure(false)//是否重连
            .connectionPool(ConnectionPool(8, 15, TimeUnit.SECONDS))
//            .addInterceptor(getLoggingInterceptor())//日志打印
            .addInterceptor(AndroidLoggingInterceptor.build())
            .addInterceptor(RequestInterceptor())//请求参数加密
            .addInterceptor(OkHttpProfilerInterceptor())
            .build()
    }

    /**
     * 网络日志的拦截器
     * @return
     */
    private  fun getLoggingInterceptor(): LoggingInterceptor{
        //打印网络请求日志
        return LoggingInterceptor.Builder()
            .setLevel(Level.BASIC)
            .log(Platform.INFO)
            .request("请求")
            .response("响应").tag("NetTag")
            .build()
    }


    /**
     * 创建retrofit的Service对象
     * [service] 定义的retrofit的service 接口类
     */
    fun <T> create(service: Class<T>): T = retrofit.create(service)

}